//
//  WorkGuideCommonService.m
//  homeland
//
//  Created by Alawn on 13-10-24.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "WorkGuideCommonService.h"
#import "CommonWorkGuideEntity.h"
#import "WorkQueryCell.h"
#import "WorkQuerySelectEntity.h"
#import "StreetWorkEntity.h"
#import "StreetWorkGuideEntity.h"
#import "OrgListItem.h"
#import "StreetWorkGuideOrgEntity.h"
#import "StreetGuidDetailController.h"
#import "StreetGuideDetailWebViewController.h"

@implementation WorkGuideCommonService
-(id)init
{
    self = [super init];
    if (self) {
//        _dataItemName = @"CommonWorkGuideEntity";
        self.fastSearchType = -1;//初始化
    }
    return self;
}
-(void)loadDataWithKey:(NSString *)keyOrNil
{
    _currentPageNo = 1;//刷新或查询的时候将请求页码变为1
    if (self.dataList) {
        self.dataList = nil;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    if (keyOrNil) {
//        [params setValue:keyOrNil forKey:@"title"];
//    } else {
//        [params setValue:@"" forKey:@"title"];
//    }
//     WorkQuerySelectEntity *item = (WorkQuerySelectEntity *)self.dataItem;
//    [params setValue:item.categoryCode forKey:@"categoryCode"];
//    [params setValue:item.ztsxfl forKey:@"ztsxfl"];
//    [params setValue:item.sjfl forKey:@"sjfl"];
//    [params setValue:item.tddx forKey:@"tddx"];
//    [self doHttpPost:SERVER_URL_GUIDE headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo] parameters:params];
    if ([self.dataItem isKindOfClass:[StreetWorkEntity class]] || self.fastSearchType == 0) {
        //按街道筛选
        _dataItemName = @"StreetWorkGuideEntity";
        if (keyOrNil) {
            [params setValue:keyOrNil forKey:@"serviceName"];
        } else {
            [params setValue:nil forKey:@"serviceName"];
        }
        StreetWorkEntity *item = (StreetWorkEntity *)self.dataItem;
        [params setValue:item.orgCode forKey:@"adminOrgCode"];
        [self doHttpPost:SERVER_URL_PEOPLE_ITEM_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo] parameters:params];
    } else if ([self.dataItem isKindOfClass:[OrgListItem class]] || self.fastSearchType == 1) {
        //按部门筛选
        _dataItemName = @"StreetWorkGuideOrgEntity";
        if (keyOrNil) {
            [params setValue:keyOrNil forKey:@"title"];
        } else {
            [params setValue:nil forKey:@"title"];
        }
        OrgListItem *item = (OrgListItem *)self.dataItem;
        [params setValue:item.Id forKey:@"orgId"];
        [self doHttpPost:SERVER_URL_PROPLR_ITEM_ORG_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo] parameters:params];
    }
}

-(void)loadMoreDataWithKey:(NSString *)keyOrNil
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    if (keyOrNil) {
//        [params setValue:keyOrNil forKey:@"title"];
//    } else {
//        [params setValue:@"" forKey:@"title"];
//    }
//    WorkQuerySelectEntity *item = (WorkQuerySelectEntity *)self.dataItem;
//    [params setValue:item.categoryCode forKey:@"categoryCode"];
//    [params setValue:item.ztsxfl forKey:@"ztsxfl"];
//    [params setValue:item.sjfl forKey:@"sjfl"];
//    [params setValue:item.tddx forKey:@"tddx"];
//    [self doHttpPost:SERVER_URL_GUIDE headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo] parameters:params];
//    if (keyOrNil) {
//        [params setValue:keyOrNil forKey:@"serviceName"];
//    } else {
//        [params setValue:@"" forKey:@"serviceName"];
//    }
//    StreetWorkEntity *item = (StreetWorkEntity *)self.dataItem;
//    [params setValue:item.orgCode forKey:@"adminOrgCode"];
//    [self doHttpPost:SERVER_URL_PEOPLE_ITEM_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo] parameters:params];
    if ([self.dataItem isKindOfClass:[StreetWorkEntity class]] || self.fastSearchType == 0) {
        //按街道筛选
        _dataItemName = @"StreetWorkGuideEntity";
        if (keyOrNil) {
            [params setValue:keyOrNil forKey:@"serviceName"];
        } else {
            [params setValue:nil forKey:@"serviceName"];
        }
        StreetWorkEntity *item = (StreetWorkEntity *)self.dataItem;
        [params setValue:item.orgCode forKey:@"adminOrgCode"];
        [self doHttpPost:SERVER_URL_PEOPLE_ITEM_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo + 1] parameters:params];
    } else if ([self.dataItem isKindOfClass:[OrgListItem class]] || self.fastSearchType == 1) {
        //按部门筛选
        _dataItemName = @"StreetWorkGuideOrgEntity";
        if (keyOrNil) {
            [params setValue:keyOrNil forKey:@"title"];
        } else {
            [params setValue:nil forKey:@"title"];
        }
        OrgListItem *item = (OrgListItem *)self.dataItem;
        [params setValue:item.Id forKey:@"orgId"];
        [self doHttpPost:SERVER_URL_PROPLR_ITEM_ORG_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo + 1] parameters:params];
    }
}

-(void)configCell:(UITableViewCell *)cell atIndex:(NSIndexPath *)index
{
    
    WorkQueryCell *tempCell = (WorkQueryCell *)cell;
    
    
    if ([self.dataItem isKindOfClass:[StreetWorkEntity class]] || self.fastSearchType == 0) {
        //按街道筛选
        StreetWorkGuideEntity *item = [self.dataList objectAtIndex:index.row];
        [self configInfoWithCommonGuidCell:tempCell withItem:item atIndex:index.row];
    } else if ([self.dataItem isKindOfClass:[OrgListItem class]] || self.fastSearchType == 1) {
        //按部门筛选
        StreetWorkGuideOrgEntity *item = [self.dataList objectAtIndex:index.row];
        tempCell.mainInfoLabel.text = item.title;
        tempCell.guidButton.tag = index.row;
        if (self.delegate && [self.delegate respondsToSelector:@selector(guidCommonButtonAction:)]) {
            [tempCell.guidButton addTarget:self.delegate action:@selector(guidCommonButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

-(CGFloat)heightForCellAtRow:(NSIndexPath *)index
{
    return 64.0f;
}

-(ConfigableViewController *)getCellTargetViewControllerForName:(NSString *)className
{
    if ([self.dataItem isKindOfClass:[StreetWorkEntity class]] || self.fastSearchType == 0) {
        //按街道筛选
        StreetGuidDetailController *vc = [[StreetGuidDetailController alloc] initWithNibName:@"StreetGuidDetailController" bundle:nil];
        return vc;
    } else if ([self.dataItem isKindOfClass:[OrgListItem class]] || self.fastSearchType == 1) {
        //按部门筛选
        StreetGuideDetailWebViewController *vc = [[StreetGuideDetailWebViewController alloc] initWithNibName:@"StreetGuideDetailWebViewController" bundle:nil];
        return vc;
    }
    return nil;
}
@end
